package com.etrans.web.gps.business.report.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.etrans.web.gps.base.controller.BaseController;
import com.etrans.web.gps.business.base.service.EnterpriseService;
import com.etrans.web.gps.business.report.domain.EnterpriseRemindVo;
import com.etrans.web.gps.business.report.service.EnterpriseRemindService;
import com.etrans.web.gps.common.page.PageVo;
import com.etrans.web.gps.util.grid.FlexiGridUtil;

/** 
* @author 作者 jia: 
* @version 创建时间：2018年1月23日 下午4:39:54 
* 类说明 
*/
@Controller
@RequestMapping("/report/enterpriseRemind")
public class EnterpriseRemindController extends BaseController{
	
	@Autowired
	private EnterpriseRemindService enterpriseRemindService;
	
	@RequestMapping("/enterpriseRemind.html")
	public String show(){
		return "views/business/report/enterpriseRemind";
	}
	
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/queryByType.html")
	@ResponseBody
	public List<HashMap<String,Object>> Query(@RequestParam String flag,HttpServletRequest request){
		String name = request.getParameter("name");
		return enterpriseRemindService.queryDriverOrEnterpriseName(flag,name);
	}
	
	
   /*
    * 根据 通知方式，对象，具体的名称 ，时间段查询
    */
	@RequestMapping(value = "/loadTableData.html")
	@ResponseBody
	public PageVo loadTableData(String paramsGrid){
		PageVo vo = new PageVo();
		try {
			Map<String, Object> param = FlexiGridUtil.parseJSONParam(paramsGrid).getWhereParam();
			vo.setPage((Integer)param.get("page"));
			param.put("pageSize", param.get("pageSize"));
			param.put("page", ((Integer) param.get("page")-1)*(Integer)param.get("pageSize"));
			List<EnterpriseRemindVo> list = enterpriseRemindService.loadTableData(param);
			Integer count = enterpriseRemindService.countTableData(param);
		
			vo.setRows(list);
			vo.setTotal(count);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		return vo;
		
	}
	@RequestMapping(value="/loadChartData.html",method=RequestMethod.POST)
	@ResponseBody
	public Map<Object, Object> loadChrtData(String notifyObj,String notifyType,String name,
			      String objId,String startTime,String endTime){
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("notifyObj", notifyObj);
		params.put("notifyType", notifyType);
		params.put("startTime", startTime);
		params.put("endTime",endTime);
		params.put("objId", objId);
		params.put("name", name);
		return enterpriseRemindService.loadChartData(params);
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "exportQueryData.html")
	public void exportQueryData( HttpServletRequest request, HttpServletResponse response, String startTime, String endTime,String notifyType ,
			String notifyObj,String objId,String title, String startPage, String endPage, String PageSize,String colModel,String imgSrc) {

		List<String> colModelList = JSON.parseObject(colModel, List.class);
		List<String> imgsrcList   = JSON.parseObject(imgSrc, List.class);
		int page = Integer.parseInt(startPage);
		int EndPage = Integer.parseInt(endPage);
		int pageSize =(EndPage - page)* Integer.parseInt(PageSize);
		int fromNumber = (page-1) * Integer.parseInt(PageSize);
		Map<String,Object> param =new HashMap<>();
		param.put("notifyObj", notifyObj);
		param.put("notifyType", notifyType);
		param.put("startTime", startTime);
		param.put("endTime",endTime);
		param.put("objId", objId);
		//param.put("fromNumber",fromNumber);
		param.put("pageSize", pageSize);
		param.put("page", (page-1)*pageSize);
		List list = enterpriseRemindService.loadTableData(param);
		List<HashMap<String, Object>> rows = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < list.size(); i++) {
			HashMap<String, Object> map = new HashMap<>();
			EnterpriseRemindVo enterpriseRemindVo = (EnterpriseRemindVo) list.get(i);
			if (enterpriseRemindVo.getNotifyType() == 1 ) {
				//1 代表电话 2 代表邮件 3代表短信
				map.put("notifyType", "电话");
			}else if (enterpriseRemindVo.getNotifyType() == 2 ) {
				map.put("notifyType", "邮件");
			}else {
				map.put("notifyType", "短信");
			}
			int notifyObject = enterpriseRemindVo.getNotifyObject();
		    //1 代表企业 2代表司机
			map.put("notifyObject", notifyObject == 1?"企业":"司机");
			map.put("name", enterpriseRemindVo.getName());
			map.put("notifyContent", enterpriseRemindVo.getNotifyContent());
			map.put("createTime", enterpriseRemindVo.getCreateTime());
			rows.add(map);
		}
		int index = -1;
		List rowsList = new ArrayList<>();
		rowsList.add(rows);
		exportExl(request, response,title, removeHeadName(colModelList,index,-1),rowsList,imgsrcList);
	}
	
}
